#pragma once

#include "space\space.h"
#include "space\helpers\file.h"

#include <map>
#include <fstream>
#include <string>

#include <google\protobuf\text_format.h>


namespace space {
	
	namespace parsers {

		template <class objectType> static result load_objects(std::map<int, objectType> * list, char *folder) {
			std::vector<std::string> file_list;
			result res = RESULT_ERROR;

			// get the file list on our gamedata tech directory
			res = helpers::file::list_files(file_list, folder, "*.txt");
			if(res != RESULT_OK) {
				// TODO: need real exceptions and logging
				LOG("Failed to load directory.\n");
				throw 1;
			}

			for(auto iter = file_list.begin(); iter != file_list.end(); iter++) {
				objectType obj;

				ifstream input(((std::string)(*iter)).c_str());
	
				std::string data((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
				google::protobuf::TextFormat::ParseFromString(data.c_str(), &obj);
				TRACE("Loaded object: %d:[%s]\n", obj.id(), obj.name().c_str());

				list->insert( pair<int, objectType>(obj.id(), obj) );
			}

			return RESULT_OK;
		}
	};

};